package main

import (
	"math/rand"
	"time"
	"fmt"
)

/*
√ 随机招收一些学生进班
√ 随机生成学生的成绩
√ 对学生成绩排名并输出
√ 封装一个对全班学生成绩进行排名的函数
----------
√ 将上述封装好的两个函数丢入mylib包中
√ 在当前工程中调用mylib包内的函数
√ 在其它工程中调用mylib包内的函数
*/

func SortSlice(s []int) {
	for i := 0; i < len(s)-1; i++ {
		for j := i + 1; j < len(s); j++ {
			if s[j] > s[i] {
				s[i], s[j] = s[j], s[i]
			}
		}
	}
}

func GetRandomInt(n int) int {
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	return r.Intn(n)
}

func ShowScoreRanking(sm map[string]int) {
	s := make([]string, 0)
	for name := range sm {
		s = append(s, name)
	}
	for i := 0; i < len(s)-1; i++ {
		for j := i + 1; j < len(s); j++ {
			if sm[s[j]] > sm[s[i]] {
				s[i], s[j] = s[j], s[i]
			}
		}
	}
	for i,name := range s{
		fmt.Printf("第%2d名：%4s\t %3d\n",i+1,name,sm[name])
	}
}

func main() {
	names := make([]string, 0)
	names = append(names, "张三丰", "李师师", "伍子胥", "杨六郎", "洪七公", "朱重八", "马英九", "十三姨")
	scoreMap := make(map[string]int)

	for _, name := range names {
		scoreMap[name] = GetRandomInt(100)
		time.Sleep(1 * time.Nanosecond)
	}
	fmt.Println(scoreMap)

	ShowScoreRanking(scoreMap)
}
